package com.zian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zian.context.BaseContext;
import com.zian.domain.dto.CarouselDTO;
import com.zian.domain.dto.PageDTO;
import com.zian.domain.po.SysCarousel;
import com.zian.domain.query.PageQuery;
import com.zian.exception.BadRequestException;
import com.zian.exception.DbException;
import com.zian.mapper.SysCarouselMapper;
import com.zian.service.ISysCarouselService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zian.utils.RedisUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024-09-08
 */
@Service
@RequiredArgsConstructor
public class SysCarouselServiceImpl extends ServiceImpl<SysCarouselMapper, SysCarousel> implements ISysCarouselService {

    private final RedisUtils redisUtils;

    @Override
    public void addCarousel(CarouselDTO dto) {
        Long currentId = BaseContext.getCurrentId();
        SysCarousel carousel = BeanUtil.toBean(dto, SysCarousel.class);
        carousel.setUpdateUser(currentId.intValue());
        carousel.setCreateUser(currentId.intValue());
        boolean b = save(carousel);
        if (!b) throw new DbException("添加轮播图失败");
        redisUtils.deleteObject("carouselCache");
    }

    @Override
    public PageDTO<SysCarousel> queryPage(PageQuery query) {
        Serializable cacheObject = redisUtils.getCacheObject("carouselCache");
        if (cacheObject == null) {
            Page<SysCarousel> page = query.toMpPage("carousel_rank", false);
            lambdaQuery()
                    .page(page);
            if (page.getRecords().isEmpty()) return PageDTO.empty(0L, 0L);
            PageDTO<SysCarousel> dto = PageDTO.of(page);
            redisUtils.setCacheObject("carouselCache", dto);
            return dto;
        }
        PageDTO<SysCarousel> dto = (PageDTO<SysCarousel>) cacheObject;
        return dto;
    }

    @Override
    public void updateCarousel(CarouselDTO dto) {
        // 修改
        SysCarousel sysCarousel = getById(dto.getId());
        if (sysCarousel == null) throw new BadRequestException("轮播图不存在");
        sysCarousel.setCarouselRank(dto.getCarouselRank());
        sysCarousel.setCarouselUrl(dto.getCarouselUrl());
        sysCarousel.setRedirectUrl(dto.getRedirectUrl());
        sysCarousel.setUpdateUser(BaseContext.getCurrentId().intValue());
        sysCarousel.setUpdateTime(null);
        boolean b = updateById(sysCarousel);
        if (!b) throw new DbException("修改轮播图失败");
        redisUtils.deleteObject("carouselCache");
    }

    @Override
    @Transactional
    public void deleteCarouselByIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) throw new BadRequestException("参数错误");
        boolean b = removeByIds(ids);
        if (!b) throw new DbException("删除轮播图失败");
        redisUtils.deleteObject("carouselCache");
    }

    @Override
    public CarouselDTO getCarouselById(Long id) {
        SysCarousel carousel = getById(id);
        if (carousel == null) throw new DbException("轮播图不存在");
        return BeanUtil.toBean(carousel, CarouselDTO.class);
    }
}
